<template>
  <div class="dashboard-container">
    <div class="app-container">
      <page-tools>
        <!-- 左侧 -->
        <template #slot-left>
          <span>共 {{ total }} 条记录</span>
        </template>
        <!-- 右侧 -->
        <template #slot-right>
          <el-button type="danger" size="small" @click="$router.push('/Excel')">导入excel</el-button>
          <el-button type="success" size="small">导出excel</el-button>
          <el-button type="primary" size="small" @click="addEmployee" @close="resetForm">新增员工</el-button>

        </template>
      </page-tools>
      <el-card class="box-card">
        <el-table
          :default-sort="{prop: 'workNumber', order: 'descending'}"
          :data="employeesList"
          border
          style="width: 100%"
        >
          <el-table-column type="index" label="序号" />
          <el-table-column prop="username" label="姓名" />
          <el-table-column prop="staffPhoto" label="头像" />
          <el-table-column prop="mobile" label="手机号" />
          <el-table-column prop="workNumber" label="工号" sortable />
          <el-table-column prop="formOfEmployment" label="聘用形式">
            <template slot-scope="scope">
              {{ scope.row.formOfEmployment |conversionFormat }}
            </template>
          </el-table-column>
          <el-table-column prop="departmentName" label="部门" />
          <el-table-column prop="correctionTime" label="入职时间" />
          <el-table-column prop="formOfEmployment" label="账户状态" />
          <el-table-column label="操作" width="280">
            <template slot-scope="scope">
              <el-button type="text" size="small">查看</el-button>
              <el-button type="text" size="small">分配角色</el-button>
              <el-button type="text" size="small" @click="delEmployee(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          :current-page="query.page"
          :page-sizes="[4, 10, 15, 20]"
          :page-size="query.size"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
        <el-dialog
          title="员工信息"
          type="primary"
          :visible.sync="showDialog"
          width="50%"
          @close="resetForm"
        >

          <emp-dialog ref="updateForm" @cancel="showDialog=false" @shutDown="renewList" />

        </el-dialog>
      </el-card>
    </div>
  </div>
</template>

<script>
// import PageTools from '@/components/PageTools/index.vue'
import { getAListOfEmployees, delEmployees } from '@/api/employees'
import conversionConst from '@/constant/employees.js'
import empDialog from './empDialog.vue'
// console.log(conversionConst)
export default {
  name: 'Employees',
  components: {
    // PageTools,
    empDialog
  },
  filters: {
    //   转换格式
    conversionFormat(val) {
      const res = conversionConst.hireType.find(item => item.id === Number(val))
      if (res) {
        return res.value
      } else {
        return '未定义'
      }
    }
  },
  data() {
    return {
      query: {
        size: 10,
        page: 1
      },
      total: 0, // 总条数
      employeesList: [], // 员工列表
      showDialog: false
    }
  },
  created() {
    this.getAListOfEmployees()
  },
  methods: {

    renewList() {
      this.showDialog = false
      this.getAListOfEmployees()
    },
    // 重置表单
    resetForm() {
      this.$refs.updateForm.formReset()
    },
    //   新增员工
    addEmployee() {
      this.showDialog = true
    },
    //   删除员工
    async delEmployee(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await delEmployees(id)
        this.getAListOfEmployees()
        if (this.employeesList.length === 1) {
          this.query.page--
          if (this.query.page < 1) {
            this.query.page = 1
          }
        }
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    //   console.log(res)
    },
    //   获取员工列表
    async getAListOfEmployees() {
      const res = await getAListOfEmployees(this.query)
      this.employeesList = res.data.rows
      this.total = res.data.total
    //   console.log(res)
    },
    // 每页条数发生改变时触发
    handleSizeChange(newSize) {
      this.query.size = newSize
      this.getAListOfEmployees()
    },

    // 页码发生改变时触发
    handleCurrentChange(newPage) {
      this.query.page = newPage
      this.getAListOfEmployees()
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
